package net.zjitc.service;

import net.zjitc.mapper.UserMapper;
import net.zjitc.po.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * Created by Administrator on 2018/1/3.
 */
public class UserService {
    SqlSessionFactory factory;
    public UserService(){
             /*读取资源配置文件*/
        InputStream in = null;
        try {
            in = Resources.getResourceAsStream("mybatis-config.xml");
        } catch (IOException e) {
            e.printStackTrace();
        }
        /*根据资源文件生成SessionFactory*/
        factory = new SqlSessionFactoryBuilder().build(in);
        /*从session工厂中打开session会话*/
    }

    public User findUsernameAndPassword(String unsenamw,String password) {//方法名及返回类型要与mapper包接口下的一致
        SqlSession sqlSession = factory.openSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);//
        User list = mapper.findUsernameAndPassword(unsenamw,password);
        sqlSession.close();
        return list;
    }

    public int addUser(User user) {
        SqlSession sqlSession = factory.openSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);//
        int num= mapper.addUser(user);
        sqlSession.commit();
        sqlSession.close();
        return num;
    }
}
